package com.example.apple.ddhr_new.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.activity.bean.SysTemBean;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;

import com.suke.widget.SwitchButton;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Optional;

public class SystemAdapter extends GroupedRecyclerViewAdapter {

    private static final int TYPESWITCH = 100 ;
    private static final int TYPESUBTITLE = 101;
    private static final int TYPESUBTITLEDFAULT = 102;
    public  OnCheckedChangeListener checkedChangeListener;
    private  RecycleViewType recycleType;
    public SystemAdapter(Context context) {
        this(context,RecycleViewType.DEFAULT);

    }
    private ArrayList data = new ArrayList();

    public ArrayList getData() {
        return data;
    }

    public void setData(ArrayList data) {
        this.data = data;
    }

    public SystemAdapter(Context context, RecycleViewType recycleType) {
        super(context);
        this.recycleType = recycleType;
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return recycleType==RecycleViewType.GROUP;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getGroupCount() {

        if(recycleType==RecycleViewType.GROUP){
            return  this.getData().size();
        }else {

            return 1;
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        if(recycleType==RecycleViewType.GROUP){
            return  ((List)this.getData().get(groupPosition)).size();
        }else {

          return   getData().size();
        }

    }

    @Override
    public int getHeaderLayout(int viewType) {
        return  R.layout.header_system;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        if(viewType==TYPESUBTITLE){

         return    R.layout.item_subtitle;

        } else if(viewType==TYPESWITCH){
            return    R.layout.item_switch;
        }else if(viewType==TYPESUBTITLEDFAULT){
            return    R.layout.item_subtitle_default;
        }

        return R.layout.item_setting;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {

        SettingViewHolder holder1 = (SettingViewHolder) holder;
        if(recycleType==RecycleViewType.GROUP){
            SysTemBean bean =   ((List<SysTemBean>)this.getData().get(groupPosition)).get(childPosition);
            holder1.setData(bean);
            if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SWITCH){

                holder1.btnSwitch.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                        if(null!=checkedChangeListener){

                            checkedChangeListener.onCheckedChanged(groupPosition,childPosition,isChecked);
                        }
                    }
                });
            }else if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_ARROW){

                holder1.subtitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(null!=checkedChangeListener){
                            checkedChangeListener.onSubTitleClick(groupPosition,childPosition);
                        }
                    }
                });
            }
        }else {

            SysTemBean bean =   ((List<SysTemBean>)this.getData().get(groupPosition)).get(childPosition);

            holder1.setData(bean);
            if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SWITCH){

                holder1.btnSwitch.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                        if(null!=checkedChangeListener){
                            checkedChangeListener.onCheckedChanged(groupPosition,childPosition,isChecked);
                        }
                    }
                });
            }else if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_ARROW){

                holder1.subtitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(null!=checkedChangeListener){
                            checkedChangeListener.onSubTitleClick(groupPosition,childPosition);
                        }
                    }
                });
            }
        }




    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPESUBTITLE){

            View view = LayoutInflater.from(mContext).inflate(getChildLayout(viewType), parent, false);

            return  new SettingViewHolder(view);

        } else if(viewType==TYPESWITCH){
            View view = LayoutInflater.from(mContext).inflate(getChildLayout(viewType), parent, false);

            return  new SettingViewHolder(view);

        } else if(viewType==TYPESUBTITLEDFAULT){
            View view = LayoutInflater.from(mContext).inflate(getChildLayout(viewType), parent, false);

            return  new SettingViewHolder(view);

        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public int getChildViewType(int groupPosition, int childPosition) {
        if(recycleType==RecycleViewType.GROUP){
            SysTemBean bean =   ((List<SysTemBean>)this.getData().get(groupPosition)).get(childPosition);
            if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SWITCH){
                return TYPESWITCH;
            }else  if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_ARROW){
                return TYPESUBTITLE;
            }else  if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_DEFAULT){
                return TYPESUBTITLEDFAULT;
            }

        }else {

            SysTemBean bean =   ((List<SysTemBean>)this.getData().get(groupPosition)).get(childPosition);
            if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SWITCH){
                return TYPESWITCH;
            }else  if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_ARROW){
                return TYPESUBTITLE;
            }else  if(bean.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_DEFAULT){
                return TYPESUBTITLEDFAULT;
            }
        }

        return TYPE_CHILD;
    }





    public class SettingViewHolder extends BaseViewHolder{


        @Nullable
        @BindView(R.id.tv_title)
        TextView title;
        @Nullable
        @BindView(R.id.tv_subtitle)
        TextView subtitle;
        @Nullable
        @BindView(R.id.btn_switch)
        SwitchButton btnSwitch;


        public SettingViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setData(SysTemBean data) {

            if(data.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_ARROW){
                title.setText(data.sysTitle);
                subtitle.setText(data.subTitle);
            }else if(data.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SWITCH){
                title.setText(data.sysTitle);
                btnSwitch.setChecked(data.isSelected);


            }else if(data.sysTemBeanType==SysTemBean.SysTemBeanType.TYPE_SUBTITLE_DEFAULT){
                title.setText(data.sysTitle);
                subtitle.setText(data.subTitle);
            }




        }


    }

    public   enum RecycleViewType {
        DEFAULT,GROUP
    }

    public   interface   OnCheckedChangeListener{

        public void  onCheckedChanged(int groupPosition, int childPosition, boolean isChecked);
        public void  onSubTitleClick(int groupPosition, int childPosition);
    }
}
